package com.weipt.juc.syncollector;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

/**
 * @author peter.wei 2020/09/15 19:38
 * @version 1.0
 * @company 终生学习
 * @description <br>peter.wei 2020/09/15 19:38
 **/
public class TestCopyOnWriteSet {

    private static Set<String> set = new HashSet<>();
    private static Set<String> cowSet = new CopyOnWriteArraySet<>();


    //提示java.util.ConcurrentModificationException
    //提示数组越界
    //数据长度不符合预期，数据内容不符合预期
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                TestCopyOnWriteSet.putSet();
            }).start();
        }

        /*for (int i = 0; i < 10; i++) {
            new Thread(()->{
                TestCopyOnWriteSet.putSynSet();
            }).start();
        }*/

        /*for (int i = 0; i < 10; i++) {
            new Thread(()->{
                TestCopyOnWriteSet.putSet();
            }).start();
        }*/
        TimeUnit.SECONDS.sleep(1);  //等待5s，让子线程执行完
        System.out.println(set.size());
        System.out.println(cowSet.size());

    }

    public static void putSet(){
        for (int i = 0; i < 50; i++) {
            set.add(String.valueOf(i));
        }
    }

    public static void putCowSet(){
        for (int i = 0; i < 50; i++) {
            cowSet.add(String.valueOf(i));
        }
    }
}